<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    #box {
        width: 100px;
        height: 100px;
        position: absolute;
        left: 0;
        background-color: lightcoral;
    }
</style>

<body>
    <input type="button" id="start" value="开始">
    <input type="button" id="end" value="停止">
    <div id="box"></div>
    <script>
        var start = document.getElementById("start");
        var end = document.getElementById("end");
        var box = document.getElementById("box");

        //全局变量存储left的属性值，每次都会发生变化实现移动
        var boxleft = 0;//初始值必须与box的初始属性值一样
        var boxInveral;
        start.onclick = function () {
            //定时器写在事件函数内部，当事件多次被触发，会导致定时器累积
            //解决方法：设表先关
            clearInterval(boxInveral);//关闭之前的定时器
            boxInveral = setInterval(function () {
                boxleft += 10;
                box.style.left = boxleft + "px";
            }, 100);
        };

        //清除定时器
        end.onclick = function () {
            clearInterval(boxInveral);
        }
    </script>
</body>

</html>